package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("MotiveDAO")
@Transactional
public class MotiveDAOImpl extends AbstractJpaDao<Motive> implements MotiveDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Motive.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public MotiveDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Motive findMotiveById(Integer id) throws DataAccessException {

		return findMotiveById(id, -1, -1);
	}

	@Transactional
	public Motive findMotiveById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMotiveById", startResult, maxRows, id);
			return (Motive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Motive> findMotiveByName(String name) throws DataAccessException {

		return findMotiveByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Motive> findMotiveByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMotiveByName", startResult, maxRows, name);
		return new LinkedHashSet<Motive>(query.getResultList());
	}

	@Transactional
	public Set<Motive> findMotiveByNameContaining(String name) throws DataAccessException {

		return findMotiveByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Motive> findMotiveByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMotiveByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Motive>(query.getResultList());
	}

	@Transactional
	public Set<Motive> findMotiveByDescription(String description) throws DataAccessException {

		return findMotiveByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Motive> findMotiveByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMotiveByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Motive>(query.getResultList());
	}

	@Transactional
	public Set<Motive> findMotiveByDescriptionContaining(String description) throws DataAccessException {

		return findMotiveByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Motive> findMotiveByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMotiveByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Motive>(query.getResultList());
	}

	@Transactional
	public Motive findMotiveByPrimaryKey(Integer id) throws DataAccessException {
		return findMotiveByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Motive findMotiveByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMotiveByPrimaryKey", startResult, maxRows, id);
			return (Motive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Motive> findMotivesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMotivesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Motive> findAllMotives() throws DataAccessException {

		return findAllMotives(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Motive> findAllMotives(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllMotives", startResult, maxRows);
		return new LinkedHashSet<Motive>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Motive> findMotiveByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Motive>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Motive> findMotivesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Motive.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Motive> motives = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return motives;
	}

	@Transactional
	public Motive findMotiveByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findMotiveByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Motive findMotiveByPhysicalPrimaryKey(String name, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMotiveByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Motive) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Motive entity) {
		return true;
	}
}
